package com.jerry.base.thread;

import lombok.extern.slf4j.Slf4j;

/**
 * User: Jerry
 * Date: 2024/1/31
 * Time: 23:59
 * Description:
 */
@Slf4j
public class DaemonThread {

    static class Daemon implements Runnable {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("守护执行");
            }
        }
    }

    public static void main(String[] args) {
        Thread thread  = new Thread(new Daemon());
        // 在当前线程中设置某个线程为守护线程，并且必须在 start() 方法执行前设置
        thread.setDaemon(true);
        thread.start();

        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("主线程执行");
        }
    }
}
